<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Report extends User_Controller
{
	private $response = array();
	private $userModel;
	private $clientModel;
	private $requestManager;
	private $settings;
    public function __construct()
    {
        parent::__construct();
        
   	 if(isset($this->session) && !$this->session->userdata('isLogged')) {
        	redirect('/');
        }        
        $this->userModel = $this->load->model('User_Model');
        $this->clientModel = $this->load->model('Client_Model');
        $this->requestManager = RequestManager::getInstance();
        $this->settings = $this->config->item('settings');
        $this->dateUtils = $this->load->helper('DateUtils');
		$this->fpdf = $this->load->helper('FPDF');
		$this->load->helper(array('ConstantsUtils','StringUtils','PHPExport'));
    }

    public function index($payroll_id = 0){
		$data = array();
    	if(is_numeric($payroll_id) && $payroll_id > 0) {
    		//$payroll_id = $this->input->post('period');
			$this->requestManager->setData(array('payroll_id'=>$payroll_id));
			$this->requestManager->setSendType("rest");
			$this->requestManager->setAccessPath($this->settings['api_path']."payroll/get_payroll/" .$this->settings['client_key']);
			$return = $this->requestManager->send();
			if($return->message == 'success'){
				$data['message'] = $return->message;
				$data['timesheet'] = $return->data;
				$data['not_reviewed'] = $return->not_reviewed;
				$data['reviewed'] = $return->reviewed;
			}
    	}
		
		// get all payroll periods
		$this->requestManager->setData(array());
		$this->requestManager->setSendType("rest");
		$this->requestManager->setAccessPath($this->settings['api_path']."payroll/client_payroll_periods/" .$this->settings['client_key']);
		$return = $this->requestManager->send();
		if($return->message == 'success'){
			$data['periods'] = $return->data;
			
		
		}
		$data['payroll_id'] = $payroll_id;
    	$data['message'] = "Report Page";
    	$this->load->view('header');
		$this->load->view('index',$data);
		$this->load->view('footer');
    }
	public function get_payroll($payroll_id){
		$data = array();
		$url = $this->settings['api_path'] ."payroll/get_payroll/" . $this->settings['client_key']."/$payroll_id";
		$json = file_get_contents($url);
		$array = json_decode($json);
		$data['message'] = $array->message;
		$data['payroll'] = $array->data;
		$this->load->view('payroll',$data);
	}
	
	public function payroll_journal(){
		$data = array();
		$this->requestManager->setData(array());
		$this->requestManager->setSendType("rest");
		$this->requestManager->setAccessPath($this->settings['api_path']."payroll/client_payroll_periods/" .$this->settings['client_key']);
		$return = $this->requestManager->send();
		if($return->message == 'success'){
			$data['periods'] = $return->data;
		}
		$this->load->view('header');
		$this->load->view('payroll_journal',$data);
		$this->load->view('footer');
	}
	
	public function payslip(){
		$data = array();
		$this->requestManager->setData(array());
		$this->requestManager->setSendType("rest");
		$this->requestManager->setAccessPath($this->settings['api_path']."payroll/client_payroll_periods/" .$this->settings['client_key']);
		$return = $this->requestManager->send();
		if($return->message == 'success'){
			$data['periods'] = $return->data;
		}
		
		// get Users
		$this->requestManager->setSendType("rest");
		$this->requestManager->setAccessPath($this->settings['api_path']."user/display_members/" .$this->settings['client_key']);
		$return = $this->requestManager->send();
		$data['users'] = $return->data;
		
		$this->load->view('header');
		$this->load->view('payslip',$data);
		$this->load->view('footer');
	}
	public function export(){
		$payroll_period_id = $this->input->post('payroll_period_id');
		$template = $this->input->post('template');
		//echo "$period_id = $template";
		//$payroll_period_id = 3;
		//$template = 'payroll_journal';
		switch($template){
			
			case 'employee_timesheet':
				$user_id = $this->input->post('user_id');
				redirect($this->settings['api_path']."report/export/" .$this->settings['client_key'] ."/$payroll_period_id/$template/$user_id");
				die();
				break;
				
			case 'payroll_journal':
				redirect($this->settings['api_path']."report/export/" .$this->settings['client_key'] ."/$payroll_period_id/$template");
				die();
				break;
				
			case 'payslip':
				$user_id = $this->input->post('user_id');
				$url = $this->settings['api_path']."report/export/" .$this->settings['client_key'] ."/$payroll_period_id/$template";
				if(is_numeric($user_id)){
					$url .="/{$user_id}";
				}
				redirect($url);
				die();
				break;
		}
		
		var_dump($return);
		/*if($return->message == 'success'){
			$data['items'] = $return->data;
			$phpExport = PHPExport::getInstance();
			$phpExport->setFormat('excel');
			$phpExport->setData($return->data); 
			$phpExport->setTemplate('payroll_journal');
			$phpExport->send();
		}*/
		
		
		
		$this->load->view('header');
		$this->load->view('payroll_journal',$data);
		$this->load->view('footer');
		
		
	}
	
	public function generatePDF(){
		$payroll_id = $this->input->post("period");
		$url = $this->settings['api_path'] ."payroll/get_payroll/" . $this->settings['client_key']."/$payroll_id";
		$json = file_get_contents($url);
		$array = json_decode($json);
		$data['message'] = $array->message;
		//if($array->message <>'success')
		$data['payroll'] = $array->data;	
		print $payroll_id;
		print "<pre>";
		print_r($data);
		print "</pre>"; 
		//print $data['payroll'][0]->user_id;
		/*
					Array
(
    [payroll] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1
                    [user_id] => 430
                    [gross_salary] => 
                    [net_pay] => 
                    [payroll_id] => 1
                    [regular_hours] => 112.00
                    [regular_overtime] => 75.42
                    [regular_night_differential] => 10.00
                    [restday] => 16.00
                    [excess_restday] => 8.00
                    [restday_night_differential] => 5.00
                    [special_holiday] => 2.00
                    [excess_special_holiday] => 13.50
                    [special_holiday_night_differential] => 2.00
                    [special_holiday_restday] => 0.00
                    [excess_special_holiday_restday] => 0.00
                    [legal_holiday] => 8.00
                    [excess_legal_holiday] => 1.00
                    [legal_holiday_unwork] => 0.00
                    [legal_holiday_restday] => 0.00
                    [excess_legal_holiday_restday] => 0.00
                    [legal_holiday_night_differential] => 5.00
                    [adjustment] => 0.00
                    [incentives] => 0.00
                    [absenses_deductions] => 
                    [hours_late] => 2.00
                    [hours_deductions] => 
                    [approved_late] => 0.00
                    [approved_undertime] => 0.00
                    [tax] => 
                    [sss] => 0.00
                    [hdmf] => 0.00
                    [philhealth] => 0.00
                    [sss_loan] => 0.00
                    [hdmf_loan] => 
                    [day_absenses] => 
                    [other_deductions] => 
                    [status] => not_reviewed
                    [first_name] => Abegail Marie A.
                    [middle_name] => 
                    [last_name] => 
                    [earnings] => 10962.5022
                )

        )

)
		*/
		
		$payroll = array(
			array('Regular Hours',$data['payroll'][0]->regular_hours),
			array('Regular Overtime',$data['payroll'][0]->regular_overtime),
			array('Regular Night Differential',$data['payroll'][0]->regular_night_differential)
		);
		
		
		
		$pdf = new FPDF();
		$pdf->AddPage();
		$pdf->SetFont('Arial','B',10);
		$pdf->Cell(40,7,$data['payroll'][0]->first_name);
		$pdf->Ln();
		$pdf->SetFont('Arial','',10);
		$header = array('', 'Amount');
		foreach($header as $col)
			$pdf->Cell(90,6,$col,1);
		$pdf->Ln();
		foreach($payroll as $row)
		{
			foreach($row as $col)
				$pdf->Cell(90,6,$col,1);
			$pdf->Ln();
		}
		$pdf->Output();
		
	}
	
}